package jia.game;

import jia.core.characters.GameCharacter;

import javax.swing.table.AbstractTableModel;
import java.util.Vector;

/**
 * Custom tablehandler class that extends the AbstractTableModel.  This is intended to
 * facilitate easier insertion of data in the PlayerCharacterList component
 *
 * @author Erik Stell
 * @version 1.0
 * @see AbstractTableModel
 * @see PlayerCharacterList
 * @since 01/18/2006
 */
public class TableHandler extends AbstractTableModel {
    
    private Vector<String[]> dataRows;
    
    /**
     * Default Constructor
     */
    public TableHandler() {
        dataRows = new Vector<String[]>(); // setup the vector ]
    }
    
    /**
     * Adds a row to the table.  The player character list only
     * contains 3 columns, so this method only takes 3 string arguments
     *
     * @param a - Column 1 value
     * @param b - Column 2 value
     * @param c - Column 3 value
     */
    public void addRowToTable(String a, String b, String c) {
        String[] rowData = new String[3];
        
        rowData[0] = a;
        rowData[1] = b;
        rowData[2] = c;
        
        // Add the data to a vector
        dataRows.addElement(rowData);
        
        // Tell the table there is new data
        fireTableChanged(null);
    }
    
    public void updateTableRow(int rowIndex, String a, String b, String c){
        String[] rowData = new String[3];
        rowData[0] = a;
        rowData[1] = b;
        rowData[2] = c;
        
        dataRows.setElementAt(rowData, rowIndex);
        
        fireTableChanged(null);
        
    }

    /**
     *
     * @param rowIndex
     * @param character
     */
    public void updateTableRow(int rowIndex, GameCharacter character){
        String[] rowData = new String[3];
        rowData[0] = character.getCharacterName();
        rowData[1] = character.getPlayerClass().getPlayerClassName();
        rowData[2] = Integer.toString(character.getCharacterHP());
        
        dataRows.setElementAt(rowData, rowIndex);
        
        fireTableChanged(null);
    }
    
    /**
     * Returns the number of columns for this table as an int.  Hard coded to
     * 3 for the purposes of being used in the PlayerCharacterList component
     *
     * @return
     */
    public int getColumnCount() {
        return 3;
    }
    
    /**
     * Returns the number of rows that currently have data for this table as an int.
     *
     * @return
     */
    public int getRowCount() {
        if (dataRows != null) {
            return dataRows.size();
        } else {
            return -1;
        }
    }
    
    /**
     * Targets a specific cell in the table and returns its value as an Object
     *
     * @param row
     * @param column
     * @return
     */
    public Object getValueAt(int row, int column) {
        if (dataRows != null) {
            return ((dataRows.elementAt(row)))[column];
        } else {
            return null;
        }
    }
}